// run: $exec < input
#include <iostream>

int const maxn = 50;
long long const mo = (1ll) << 32;
long long f[maxn];
long long n, m;

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> n >> m;
	f[1] = m;
	int len = 1;
	for (int i = 2; i <= n; i += 2) {
		f[i] = (f[i - 1] * m - f[len++]) % mo;
		f[i] = (f[i] + mo) % mo;
		f[i + 1] = (f[i] * m) % mo;
	}
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = (ans + f[i]) % mo;
	std::cout << ans << '\n';
	for (int i = 1; i <= n; i++) std::cout << f[i] << ' ';
}

